{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# tensorboard的网络结构"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Extracting MNIST_data\\train-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\train-labels-idx1-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-images-idx3-ubyte.gz\n",
      "Extracting MNIST_data\\t10k-labels-idx1-ubyte.gz\n",
      "Iter0, Testing Accuracy:0.7394\n",
      "completed\n"
     ]
    }
   ],
   "source": [
    "import tensorflow as tf\n",
    "from tensorflow.examples.tutorials.mnist import input_data\n",
    "\n",
    "# 载入数据集\n",
    "mnist=input_data.read_data_sets(\"MNIST_data\",one_hot=True)\n",
    "\n",
    "# 每个批次的大小\n",
    "batch_size=200 # 每次放入的数据量\n",
    "# 计算有多少个批次\n",
    "n_batch=mnist.train.num_examples // batch_size\n",
    "\n",
    "# 命名空间\n",
    "with tf.name_scope('input'):\n",
    "   # 定义两个placeholder\n",
    "    x=tf.placeholder(tf.float32,[None,784],name='x-input')\n",
    "    y=tf.placeholder(tf.float32,[None,10],name='y-input') \n",
    "\n",
    "with tf.name_scope('layer'):\n",
    "    # 创建简单的神经网络\n",
    "    with tf.name_scope('weights'):\n",
    "        W=tf.Variable(tf.zeros([784,10]),name='W')\n",
    "    with tf.name_scope('biases'):\n",
    "        b=tf.Variable(tf.zeros([10]),name='b')\n",
    "    with tf.name_scope('wx_plus_b'):\n",
    "        wx_plus_b=tf.matmul(x,W)+b\n",
    "    with tf.name_scope('softmax'):\n",
    "        prediction=tf.nn.softmax(wx_plus_b)    \n",
    "    \n",
    "\n",
    "\n",
    "# 二次代价函数\n",
    "with tf.name_scope('loss'):\n",
    "    loss=tf.reduce_mean(tf.square(y-prediction))\n",
    "# 使用梯度下降法\n",
    "with tf.name_scope('train'):\n",
    "    train_step=tf.train.GradientDescentOptimizer(0.2).minimize(loss)\n",
    "\n",
    "# 记过存放在布尔型列表中\n",
    "with tf.name_scope('accuracy'):\n",
    "    with tf.name_scope('correct_prediction'):\n",
    "        correct_prediction=tf.equal(tf.argmax(y,1),tf.argmax(prediction,1)) # 最大值所在位置\n",
    "# 求准确率\n",
    "    with tf.name_scope('accuracy'):\n",
    "        accuracy=tf.reduce_mean(tf.cast(correct_prediction,tf.float32))\n",
    "\n",
    "with tf.Session() as sess:\n",
    "    sess.run(tf.global_variables_initializer())\n",
    "    writer=tf.summary.FileWriter('logs/',sess.graph)\n",
    "    for epoch in range(1):\n",
    "        for batch in range(n_batch):\n",
    "            batch_xs,batch_ys=mnist.train.next_batch(batch_size)\n",
    "            sess.run(train_step,feed_dict={x:batch_xs,y:batch_ys})\n",
    "        \n",
    "        acc=sess.run(accuracy,feed_dict={x:mnist.test.images,y:mnist.test.labels})\n",
    "        print(\"Iter\"+str(epoch)+\", Testing Accuracy:\"+str(acc))\n",
    "\n",
    "print('completed')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 查看网络结构的方法\n",
    "命令行：\n",
    "```bash\n",
    "tensorboard --logdir=D:\\develop\\mygit\\noteLibrary\\TF-learning\\projector\\projector\n",
    "```\n",
    "\n",
    "在里面找到对应的路径即可看到"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
